package com.ck.controller;

import com.ck.service.AsyncThreadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

@Slf4j
@RestController
public class AsyncThreadController {

    @Resource
    private AsyncThreadService asyncThreadService;

    @GetMapping("/async")
    public String async() {
        asyncThreadService.sleep();
        log.info("{},I am main",Thread.currentThread().getName());
        return "async";
    }

    @GetMapping("/future")
    public String futureTest(){
        System.out.println("小明进店了");
        CompletableFuture.runAsync( () -> printThread("小明点了餐了"));
        System.out.println("小明在玩手机等");
        return "小明";
    }

    public static void printThread(String msg){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我等待了10秒之后出现了"+msg);
    }

}
